#include <stdint.h>
#include "config.h"
#include "apitrans.h"
#include "result.h"
#include "devapi.h"
#define LCD_ATTR_TITLE			(1)
#define LCD_ATTR_REVERSE 		(2)
#define LCD_ATTR_GRAY			(3)
#define LCD_ATTR_BACKLIGHT		(4)
#define LCD_NUM 0X08

#if defined (_IS_MPOS)            

int api_lcd_clr(unsigned char *cmd, int len)
{
	uint16_t ret = DEAL_SUCCESS;
	uint8_t offset = 0;
    
	if(len != 0){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}
	//lcd_clrscr();
	lcd_clrscr_line( 10, 64);
	lcd_set_attribute(LCD_ATTR_REVERSE, (void*)1 );
	memcpy(cmd,&ret,2);
	offset += 2;
	return offset;
}

int api_lcd_reverse(unsigned char *cmd, int len)
{
	uint8_t mod;
	uint16_t ret;
	uint8_t offset = 0;
    
	mod = cmd[offset++];
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}
	if(mod != 0 && mod != 1){
		ret = LCD_PAR_ERROR;
	}
	else{
		lcd_set_attribute( LCD_ATTR_REVERSE, (void*)mod);
		ret  = DEAL_SUCCESS;
	}
	offset = 0;
	memcpy(cmd+offset,&ret,2);
	offset += 2;
	return offset;
}
int api_lcd_backlight(unsigned char *cmd, int len)
{
	uint16_t ret ;
	uint8_t mod;
	uint8_t offset = 0;
    
	mod = cmd[offset++];
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}

	if(mod != 0 && mod != 1){
		ret = LCD_PAR_ERROR;
	}
	else{
		lcd_set_attribute(LCD_ATTR_BACKLIGHT, (void*)mod);
		ret  = DEAL_SUCCESS;
	}
	offset = 0;
	memcpy(cmd+offset,&ret,2);
	offset += 2;
	return offset;
}
int api_lcd_gary_ctrl(unsigned char *cmd, int len)
{
	uint8_t mod;
	uint16_t ret;
	uint8_t offset = 0;
    
	mod = cmd[offset++];
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}
	if(mod > 100){
		ret = LCD_PAR_ERROR;
	}
	else{
		lcd_set_attribute( LCD_ATTR_GRAY, (void*)mod);
		ret = DEAL_SUCCESS;
	}
	offset = 0;
	memcpy(cmd+offset,&ret,2);
	offset += 2;
	return offset;
}

int api_lcd_vline(unsigned char *cmd, int len)
{
	int ret1 ;
	uint16_t ret ;
	uint8_t x;
	uint8_t y;
	uint8_t v;
	uint8_t offset = 0;
    
	x = cmd[offset++];
    
	y = cmd[offset++];
    
	v = cmd[offset++];
    
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}
	ret1 = lcd_disp_vline( x, y, v);
	if(ret1 < 0){
		ret = LCD_PAR_ERROR;
		memcpy(cmd,&ret,2);
		return  -1;
	}
	else{
		ret = DEAL_SUCCESS;
	}
	offset = 0;
	memcpy(cmd+offset,&ret,2);
	offset += 2;
	return offset;
}

int api_lcd_hline(unsigned char *cmd, int len)
{
	int ret1 ;
	uint16_t ret ;
	uint8_t x;
	uint8_t y;
	uint8_t h;
	
	uint8_t offset = 0;
    
	x = cmd[offset++];
    
	y = cmd[offset++];
    
	h = cmd[offset++];
    
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}
	ret1 = lcd_disp_hline( x, y, h);
	if(ret1 < 0){
		ret = LCD_PAR_ERROR;
		memcpy(cmd,&ret,2);
		return  -1;
	}
	else{
		ret = DEAL_SUCCESS;
	}
	offset = 0;
	memcpy(cmd+offset,&ret,2);
	offset += 2;
	return offset;
}

int api_lcd_img(unsigned char *cmd, int len)
{
	int ret1 ;
	uint16_t ret ;
	uint16_t size;
	char str[256];
	uint8_t x;
	uint8_t y;
	uint8_t w;
	uint8_t h;
	uint16_t offset = 0;
    
	x = cmd[offset];
	offset += 1;
    
	y = cmd[offset];
	offset += 1;
    
	w = cmd[offset];
	offset += 1;
    
	h = cmd[offset];
	offset += 1;
    
	size = BCDtoDec(cmd+offset,2);
	offset += 2;
    
	memcpy(str, cmd+offset, size);
	offset += size;
    
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}
	if(size <= 0){
		ret = LCD_PAR_ERROR;
		memcpy(cmd,&ret,2);
		return  -1;
	}
	
	ret1 = lcd_disp_img(x,y,w,h,str);
	if(ret1 < 0){
		ret = LCD_PAR_ERROR;
		memcpy(cmd,&ret,2);
		return  -1;
	}
	else{
		ret = DEAL_SUCCESS;
	}
	offset = 0;
	memcpy(cmd+offset,&ret,2);
	offset += 2;
	return offset;

}

int api_lcd_string(unsigned char *cmd, int len)
{
	int ret1 ;
	uint16_t ret ;
	uint16_t size;
	char str[256]={0};
	uint8_t x;
	uint8_t y;
	uint16_t offset = 0;
    
	x = cmd[offset];
	offset += 1;
    
	y = cmd[offset];
	offset += 1;
    
	size = BCDtoDec(cmd+offset,2);
	offset += 2;
    
	memcpy(str,cmd+offset,size);
	offset += size;
    
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}
	if(size <= 0){
		ret = LCD_PAR_ERROR;
		memcpy(cmd,&ret,2);
		return  -1;
	}
	ret1 = lcd_disp_string( x, y, str);
	if(ret1 < 0){
		ret = LCD_PAR_ERROR;
		memcpy(cmd,&ret,2);
		return  -1;
	}
	else{
		ret = DEAL_SUCCESS;
	}
	offset = 0;
	memcpy(cmd+offset,&ret,2);
	offset += 2;
	return offset;

}

int api_lcd_dialog(unsigned char *cmd, int len)
{
	uint16_t ret = 0;
	uint8_t title[24]={0};
	uint8_t str[128]={0};
	uint8_t x,i;
	int msg_len;
	uint16_t offset = 0;
	int pos;


	memset( title, ' ', sizeof(title));
	lcd_clrscr_line( 10, 64);
	lcd_set_attribute(LCD_ATTR_REVERSE, (void*)0 );
	x = cmd[offset++];
	msg_len = cmd[offset++];
	pos = (21 - msg_len)>>1;
	memcpy(title + pos, cmd + offset, msg_len);
	offset += msg_len;
	title[21] = 0;
	lcd_disp_string( 0, 10, title);	
	lcd_set_attribute(LCD_ATTR_REVERSE, (void*)1 );



	for( i = 0; i < 3; i++)
	{
		memset( str, 0, sizeof(str));
		x = cmd[offset++];
		msg_len = cmd[offset++];
		if(msg_len > sizeof(str))
		{
			continue;
		}
		memcpy(str, cmd + offset, msg_len);
		offset += msg_len;
		lcd_disp_string( x, 24 + 13*i, str);	
	}

	offset = 0;
	memcpy(cmd+offset,&ret,2);
	offset += 2;
	return offset;

}

extern void lcd_desk();

int api_lcd_desk(unsigned char *cmd, int len)
{
	uint16_t ret = 0;
	uint16_t offset = 0;

	lcd_desk();

	offset = 0;
	memcpy(cmd+offset,&ret,2);
	offset += 2;
	return offset;

}


static func_t lcd_fun[] = {
	NULL, api_lcd_clr, api_lcd_reverse, api_lcd_backlight,\
	api_lcd_gary_ctrl, api_lcd_vline, api_lcd_hline, api_lcd_string,\
	api_lcd_img, api_lcd_dialog, api_lcd_desk
};

int cmd_lcd(uint8_t cmd, uint8_t *buf, int len)
{
	if(cmd < 0x01 || cmd > FUN_SIZE(lcd_fun))
		return -5;
	return lcd_fun[(int)cmd]((uint8_t*)buf,len);
}

#endif

